package pearson.automation.forms;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import pearson.automation.forms.courseDetails.CourseForm;
import pearson.automation.forms.note.ExportNotesForm;

import framework.elements.BaseElement;
import framework.elements.Button;
import framework.elements.ElementList;
import framework.elements.Label;
import framework.elements.Link;
import framework.elements.TextBox;

public class ActivityStreamCourse extends BaseElement {
  
    private final String tmp = "//div[@data-course-ref = '%1$s']";
    
    private String thumbnailTemplate =
        tmp + "//div[@class='course-image']";
    private Label thumbnail;
    
    private String numberOfNotificationsTemplate =
        tmp + "//span[contains(@class='notification-badge')]";
    private Label numberOfNotifications;
    
    private String courseTitleTemplate =
        tmp + "//h1[@class='course-title']";
    private Link courseTitle;
    
    private String courseStatusTemplate =
        tmp + "//p[@class='course-status']";
    private Label courseStatus;
    
    private String collaboratorsListTemplate =
        tmp + "//ul[@class='collaborators-list']/li";
    private ElementList collaboratorsList;
    
    private String editCollaboratorsTemplate =
        tmp + "//p[text()='Edit collaborators']";
    private Link editCollaborators;

    private String exportNotesTemplate =
        tmp + "//p[contains(@class, 'export-notes')]/a";
    private Link exportNotes;
    
    private String exportNotesNumberTemplate =
        tmp + "//span[@data-total-marks]";
    private Label exportNotesNumber;
    
    private String newNoteTemplate =
        tmp + "//textarea[@name='title']";
    private TextBox newNote;
    
    private String addNoteTemplate =
        tmp + "//input[@type='submit']";
    private Button addNote;
    
    private String notesListTemplate =
        tmp + "//ul[@class='marks-list']/div";
    private ElementList notesList;

    private String seeAllTemplate =
        tmp + "//a[text()='+ See All']";
    private Link seeAll;
    
    
    public ActivityStreamCourse(String courseRef) {       
        thumbnail = new Label(By.xpath(
                String.format(thumbnailTemplate, courseRef)), "Thumbnail");
        numberOfNotifications = new Label(By.xpath(
                String.format(numberOfNotificationsTemplate, courseRef)),
                "Notifications");
        courseTitle = new Link(By.xpath(
                String.format(courseTitleTemplate, courseRef)),
                "Course Title");
        courseStatus = new Label(By.xpath(
                String.format(courseStatusTemplate, courseRef)),
                "Course status");
        collaboratorsList = new ElementList(By.xpath(
                String.format(collaboratorsListTemplate, courseRef)),
                "Collaborators List");
        editCollaborators = new Link(By.xpath(
                String.format(editCollaboratorsTemplate, courseRef)),
                "Edit Collaborators");
        exportNotes = new Link(By.xpath(
                String.format(exportNotesTemplate, courseRef)),
                "Export Notes");
        exportNotesNumber = new Label(By.xpath(
                String.format(exportNotesNumberTemplate, courseRef)),
                "Export Notes Number");
        newNote = new TextBox(By.xpath(
                String.format(newNoteTemplate, courseRef)),
                "New note");
        addNote = new Button(By.xpath(
                String.format(addNoteTemplate, courseRef)),
                "Add note");
        notesList = new ElementList(By.xpath(
                String.format(notesListTemplate, courseRef)),
                "Notes List");
        seeAll = new Link(By.xpath(
                String.format(seeAllTemplate, courseRef)),
                "See All");
    }
    
    public ArrayList<String> getNotes() {
        ArrayList<String> result = new ArrayList<String>();
        
        List<WebElement> noteElements = notesList.getListElements();
        for (WebElement element: noteElements) {
            result.add(element.getAttribute("data-mark-ref"));
        }
        return result;
        
    }
    
    public void addNewNote(String noteText) {
        newNote.setText(noteText);
        addNote.clickAndWait();
    }
    
    public void waitForNoteToBeCreated() {
        newNote.waitForElementToBeLoaded();
    }
    
    public int getExportNotesNumber() {
        String exportNotesNumberString = exportNotesNumber.getText();
        try {
            return Integer.parseInt(exportNotesNumberString);
        } catch (NumberFormatException e){
            return 0;
        }
    }
    
    public List<String> getCollaboratorsList() {
        List<WebElement> collaboratorElements =
            collaboratorsList.getListElements();
        List<String> result = new ArrayList<String>();
        for (WebElement element: collaboratorElements) {
            result.add(element.getText());
        }
        return result;
    }
    
    public String getCourseStatus() {
        return courseStatus.getText();
    }
    
    public String getCourseTitle() {
        return courseTitle.getText();
    }
    
    public CourseForm openCourse() {
        courseTitle.clickAndWait();
        return new CourseForm();
    }
    
    public int getNumberOfNotifications() {
        String numberOfNotificationsString = numberOfNotifications.getText();
        try {
            return Integer.parseInt(numberOfNotificationsString);
        } catch (NumberFormatException e){
            return 0;
        }
    }
    
    public String getThumbnailURL() {
        return thumbnail.getElement().getAttribute("src");
    }

    @Override
    protected String getElementType() {
        return "Activity Stream Course";
    }

    public void clickSeeAllNotes() {
        if (seeAll.isPresent(1))
            seeAll.click();
    }

    public ExportNotesForm clickExportNotes() {
        this.exportNotes.clickAndWait();
        return new ExportNotesForm();
    }
}
